import pygame
from pygame.locals import *
import time,random



class Myplany:
	'''定义玩家飞机'''
	def __init__(self,screen_temp):
		self.x=200
		self.y=850
		self.screen=screen_temp
		self.image = pygame.image.load("./sucai/mypl.png")
		self.boom = pygame.image.load("./sucai/boom.png")
		self.fh = pygame.image.load("./sucai/fuhuo.png")
		self.bullet_list =[]#玩家子弹列表
	def display(self):
		'''绘制自己飞机'''
		
		#绘制子弹
		for b in self.bullet_list:
			b.display()
			if b.move():
				self.bullet_list.remove(b)
		self.screen.blit(self.image,(self.x,self.y))
	def move_left(self):
		'''左移飞机'''
		self.x-=8
		if self.x<=0:
			self.x=0
	def move_right(self):
		'''右移飞机'''
		self.x+=8
		if self.x>=400:
			self.x=400

	def fire(self):
		'''  发射子弹 '''
		self.bullet_list.append(Bullet(self.screen, self.x, self.y))
		print(len(self.bullet_list))
	def hurt(self):
		self.screen.blit(self.boom,(self.x,self.y))
	def over(self):
		self.screen.blit(self.fh,(150,400))
		print("游戏结束!")
		
class Bullet:
	'''子弹类'''
	def __init__(self,screen_temp,x,y):
		self.x=x
		self.y=y
		self.screen=screen_temp
		self.image = pygame.image.load("./sucai/myjig.png")
	def display(self):
		'''绘制自己飞机子弹'''
		self.screen.blit(self.image,(self.x,self.y))
		
	def move(self):
		'''子弹移动'''
		self.y-=20
		if self.y<= -40:
			return True

class Enemyplane:

	def __init__(self,screen_temp):
		self.x=random.choice(range(323))
		self.y= -135
		self.screen=screen_temp
		self.image = pygame.image.load("./sucai/a"+str(random.choice(range(1,6)))+".png")
		self.boom = pygame.image.load("./sucai/boom.png")
		self.bullet_list =[]#敌机子弹列表
	def display(self):
		'''绘制敌机'''
		#绘制敌机子弹
		for b in self.bullet_list:
			b.display()
			if b.move():
				self.bullet_list.remove(b)
		
		self.screen.blit(self.image,(self.x,self.y))

		
	def move(self,mypy):
		'''敌机移动'''
		self.y+=5
		#敌机出屏幕
		if self.y>996:
			return True
		#遍历所有子弹,并执行碰撞检测
		for bo in mypy.bullet_list:
			if bo.x>self.x-90 and bo.x<self.x+130 and bo.y>self.y+20 and bo.y<self.y+110:
				mypy.bullet_list.remove(bo)
				return True
	def fire(self):
		'''定义发射子弹'''
		self.bullet_list.append(Dbullet(self.screen, self.x, self.y))
		print(len(self.bullet_list))
	def hurt(self):
		self.screen.blit(self.boom,(self.x,self.y))

class Dbullet:
	'''敌机子弹类'''
	def __init__(self,screen_temp,x,y):
		self.x=x+90
		self.y=y+50
		self.screen=screen_temp
		self.image = pygame.image.load("./sucai/dizi.png")
	def display(self):
		'''绘制敌机飞机子弹'''
		self.screen.blit(self.image,(self.x,self.y))
		
	def move(self):
		'''子弹移动'''
		self.y+=10
		if self.y>=1100:
			return True

	
#键盘控制函数
def key_control(mypy_temp):

	#执行退出操作
	for event in pygame.event.get():
		if event.type == QUIT:
			print('exit()')
			exit()
	#获取按键消息
	pressed_key = pygame.key.get_pressed()

	if pressed_key[K_LEFT] or pressed_key[K_a]:
		print("left")
		mypy_temp.move_left()
	elif pressed_key[K_RIGHT] or pressed_key[K_d]:
		print("right")
		mypy_temp.move_right()
	if pressed_key[K_SPACE]:
		print("space")
		mypy_temp.fire()

def main():
	#创建游戏窗口
	screen = pygame.display.set_mode((520,996),0,0)
	#创建一个游戏背景
	background = pygame.image.load("./sucai/bg.jpg")
	#创建玩家飞机
	mypy = Myplany(screen)
	m = -2000
	enemylist = []#存放敌机列表
	
	while True:
		#绘制画面
		screen.blit(background,(0,m))
		m+=2
		if m>=-502:
			m = -2000


		#绘制玩家飞机
		mypy.display()

		#执行键盘控制
		key_control(mypy)

		#随机绘制敌机
		if random.choice(range(30))==15:
			enemylist.append(Enemyplane(screen))
		
		#遍历敌机并绘制和移动
		for em in enemylist:
			em.display()
			if random.choice(range(100))==10:#随机发射子弹
				
				em.fire()

			if em.move(mypy):
				em.hurt()
				enemylist.remove(em)
		
		
			#判断敌机是否与英雄飞机碰撞
			if (em.x+191>mypy.x and em.y+100>mypy.y and em.x<mypy.x+100 and em.y<mypy.y+80):
				mypy.hurt()
				mypy.over()
			#判断英雄飞机是否中弹	
			for bo in em.bullet_list:
				if (bo.x>mypy.x and bo.x<mypy.x+102 and bo.y+90>mypy.y and bo.y<mypy.y+100):
					em.bullet_list.remove(bo)
					mypy.hurt()
					mypy.over()
					
					
		#更新显示
		pygame.display.update()
		#d定时显示
		time.sleep(0.04)
#判断当前是否是主运行程序，并调用
if __name__ =="__main__":
	main()